package com.jon.adventureworks.utils
{
	import flash.net.Responder;
	import flash.system.Security;
	
	public class ServiceUtil
	{
		private static var _instance:ServiceUtil;
		
		public var gatewayURL:String;
		private var _gateway:RemotingConnection;
		private var _response:Responder;
		
		public function ServiceUtil(enforcer:SingletonEnforcer)
		{
			if (ServiceUtil._instance == null)
			{
				ServiceUtil._instance = new ServiceUtil(new SingletonEnforcer);
			}
			return ServiceUtil._instance;
		}
		
		// Set the URL for the gateway at init.
		public function init(url:String, security:Boolean, policy:String):void
		{
			if (security)
			{
				flash.system.Security.loadPolicyFile(policy);	
			}
			flash.system.Security.loadPolicyFile(url);
		}

		// Call to AMF Service.
		public function call(service:String, resultHandler:Function, faultHandler:Function, ... params):void
		{
			_response = new Responder(resultHandler, faultHandler);
			_gateway = new RemotingConnection(gatewayURL);
			_gateway.call(service, _response, params);	
		}
	}
	class SingletonEnforcer{}
}